package Quartz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.xml.crypto.Data;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;

import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;


import Eclass.Schedule.ScheduleModel;

public class QuartzPlugin implements PlugIn {
	QuartzModel schedulerModel = new QuartzModel();
	QuartzManager schedulerManager = new QuartzManager();
	Scheduler scheduler;
	String jobIdOpen = "", jobIdClosed = "",jobGroupOpen="",jobGroupClosed="";

	@Override
	public void destroy() {
		// null
	}

	public void updateOpen() {
		
	}

	public void DeleteAction(QuartzModel quartzModel) {
		jobGroupOpen+="EXAMOPEN"+quartzModel.getExamPrepId();
		jobGroupClosed+="EXAMCLOSED"+ quartzModel.getExamPrepId();
		jobIdOpen += "OPEN+" + quartzModel.getExamPrepId();
		jobIdClosed += "CLOSED+" + quartzModel.getExamPrepId();
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.deleteJob(JobKey(jobIdOpen, jobGroupOpen));
			scheduler.deleteJob(JobKey(jobIdClosed, jobGroupClosed));
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void UpdateAction(QuartzModel quartzModel) {
		jobGroupOpen+="EXAMOPEN"+quartzModel.getExamPrepId();
		jobGroupClosed+="EXAMCLOSED"+ quartzModel.getExamPrepId();
		jobIdOpen += "OPEN+" + quartzModel.getExamPrepId();
		jobIdClosed += "CLOSED+" + quartzModel.getExamPrepId();
		if (quartzModel.getStatus().equals("0")) {
			try {
				scheduler = StdSchedulerFactory.getDefaultScheduler();
				System.out.println("INACTIVE---------");
				scheduler.deleteJob(JobKey(jobIdOpen, jobGroupOpen));
				scheduler.deleteJob(JobKey(jobIdClosed, jobGroupClosed));
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {

			try {
				scheduler = StdSchedulerFactory.getDefaultScheduler();
				scheduler.deleteJob(JobKey(jobIdOpen,
						jobGroupOpen));
				scheduler.deleteJob(JobKey(jobIdClosed,
						jobGroupClosed));
				;
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}
			Action(quartzModel);
		}
	}

	public void Action(QuartzModel quartzModel) {
		schedulerModel = quartzModel;
		JobDetail jobClosed = null;
		Trigger triggerClosed = null;
		
//		System.out.println(schedulerModel.getExamPrepId());
//		System.out.println(schedulerModel.getDateOpen());
//		System.out.println(schedulerModel.getMonthOpen());
//		System.out.println(schedulerModel.getYearOpen());
//		System.out.println(schedulerModel.getHourOpen());
//		System.out.println(schedulerModel.getMinuteOpen());
//		System.out.println(schedulerModel.getDateClosed());
//		System.out.println(schedulerModel.getMonthClosed());
//		System.out.println(schedulerModel.getYearClosed());
//		System.out.println(schedulerModel.getHourClosed());
//		System.out.println(schedulerModel.getMinuteClosed());
		
		jobIdOpen = "";
		jobIdClosed = "";
		jobGroupOpen="";
		jobGroupClosed="";
		jobGroupOpen+="EXAMOPEN"+quartzModel.getExamPrepId();
		jobIdOpen += "OPEN+" + schedulerModel.getExamPrepId();
		
		
		if(schedulerModel.getExamFor().equals("Trainee")){
			jobGroupClosed+="EXAMCLOSED"+  schedulerModel.getExamPrepId();
			jobIdClosed += "CLOSED+" + schedulerModel.getExamPrepId();
			
			
			JobDetail jobOpen = JobBuilder.newJob(QuartzOpen.class)
					.withIdentity(JobKey(jobIdOpen, jobGroupOpen)).build();
			
			Trigger triggerOpen = TriggerBuilder
					.newTrigger()
					.withIdentity(TriggerKey(jobIdOpen, jobGroupOpen))
					.withSchedule(
							CronScheduleBuilder.cronSchedule("0 "
									+ schedulerModel.getMinuteOpen() + " "
									+ schedulerModel.getHourOpen() + " "
									+ schedulerModel.getDateOpen() + " "
									+ schedulerModel.getMonthOpen() + " ? "
									+ schedulerModel.getYearOpen() + "")).build();
			
			try {
				scheduler = StdSchedulerFactory.getDefaultScheduler();
				scheduler.scheduleJob(jobOpen, triggerOpen);
				System.out.println("BerhasiLLLLLLLLLLLL BUKA");
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				System.out.println("GAGAl OPEN");
			}
		
		
		jobClosed = JobBuilder.newJob(QuartzClosed.class)
				.withIdentity(JobKey(jobIdClosed, jobGroupClosed)).build();
		
		triggerClosed = TriggerBuilder
				.newTrigger()
				.withIdentity(TriggerKey(jobIdClosed, jobGroupClosed))
				.withSchedule(
						CronScheduleBuilder.cronSchedule("0 "
								+ schedulerModel.getMinuteClosed() + " "
								+ schedulerModel.getHourClosed() + " "
								+ schedulerModel.getDateClosed() + " "
								+ schedulerModel.getMonthClosed() + " ? "
								+ schedulerModel.getYearClosed() + "")).build();
		}else{
			jobClosed = JobBuilder.newJob(QuartzClosed.class)
					.withIdentity(JobKey(jobIdOpen, jobGroupOpen)).build();
			
			triggerClosed = TriggerBuilder
					.newTrigger()
					.withIdentity(TriggerKey(jobIdOpen, jobGroupOpen))
					.withSchedule(
							CronScheduleBuilder.cronSchedule("0 "
									+ schedulerModel.getMinuteOpen() + " "
									+ schedulerModel.getHourOpen() + " "
									+ schedulerModel.getDateOpen() + " "
									+ schedulerModel.getMonthOpen() + " ? "
									+ schedulerModel.getYearOpen() + "")).build();
		}
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.scheduleJob(jobClosed, triggerClosed);
			System.out.println("BerhasiLLLLLLLLLLLL TUTUP");
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("GAGAl CLOSED");
			schedulerManager.updateOpen(quartzModel.getExamPrepId(), "0");
		}
		

	}

	public void ActionCandidate(QuartzModel quartzModel){
		schedulerModel = quartzModel;
		jobIdClosed = "";
		jobGroupClosed="";
		jobGroupClosed+="EXAMCLOSED"+  schedulerModel.getUserId();
		jobIdClosed += schedulerModel.getExamFor()+"+"+ schedulerModel.getUserId()+"+"+schedulerModel.getExamPrepId();
		
		System.out.println(schedulerModel.getDateClosed());
		System.out.println(schedulerModel.getMonthClosed());
		System.out.println(schedulerModel.getYearClosed());
		System.out.println(schedulerModel.getHourClosed());
		System.out.println(schedulerModel.getMinuteClosed());
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.deleteJob(JobKey(jobIdClosed, jobGroupClosed));
		} catch (SchedulerException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		JobDetail jobClosed = JobBuilder.newJob(QuartzClosed.class)
				.withIdentity(JobKey(jobIdClosed, jobGroupClosed)).build();
		
		Trigger triggerClosed = TriggerBuilder
				.newTrigger()
				.withIdentity(TriggerKey(jobIdClosed, jobGroupClosed))
				.withSchedule(
						CronScheduleBuilder.cronSchedule("0 "
								+ schedulerModel.getMinuteClosed() + " "
								+ schedulerModel.getHourClosed() + " "
								+ schedulerModel.getDateClosed() + " "
								+ schedulerModel.getMonthClosed() + " ? "
								+ schedulerModel.getYearClosed() + "")).build();
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.deleteJob(JobKey(jobIdClosed, jobGroupClosed));
			scheduler.scheduleJob(jobClosed, triggerClosed);
			System.out.println("BerhasiLLLLLLLLLLLL TUTUP"+schedulerModel.getUserId());
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("GAGAl CLOSED UNTUK userID"+schedulerModel.getUserId());
		}
		
		//CandidateClosed=schedulerModel.get
		
	}
	
	public void ActionResetRegNo(){
		JobDetail jobReset = JobBuilder.newJob(QuartzClosed.class)
				.withIdentity(JobKey("ResetRegNo", "ResetRegNoGroup")).build();
		Trigger triggerReset = TriggerBuilder
				.newTrigger()
				.withIdentity(TriggerKey("ResetRegNo","ResetRegNoGroup"))
				.withSchedule(
						CronScheduleBuilder.cronSchedule("0 0 0 1 * ?")).build();
		
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.deleteJob(JobKey("ResetRegNo", "ResetRegNoGroup"));
			scheduler.scheduleJob(jobReset, triggerReset);
			System.out.println("BerhasiL buat RESET REG NUMBER");
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("GAGAl CLOSED UNTUK RESET REG NUMBER");
		}
	}
	
	private JobKey JobKey(String name, String group) {
		return new JobKey(name, group);
	}

	private TriggerKey TriggerKey(String name, String group) {
		return new TriggerKey(name,group);
	}

	@Override
	//reload saat server hidup kembali--REVIVE!!
	public void init(ActionServlet servlet, ModuleConfig config)
			throws ServletException {
		
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.start();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List result =new ArrayList();
		try{
		Integer i;
		for(i=0;i<2;i++){
			result=schedulerManager.selectExam(i.toString());
	//		result.add(schedulerManager.selectExam());
			System.out.println("HASIL PLUGIN"+result);
			if(result.size()!=0)
			{
				for(int x=0;x<result.size();x++){
					QuartzModel schedulerModel=(QuartzModel) result.get(x);
					
					String[] open = schedulerModel.getStartHour().split(":");
					String[] closed = schedulerModel.getEndHour().split(":");
					
					schedulerModel.setExamFor("Trainee");
					schedulerModel.setDateOpen(open[0]);
					schedulerModel.setMonthOpen(open[1]);
					schedulerModel.setYearOpen(open[2]);
					schedulerModel.setHourOpen(open[3]);
					schedulerModel.setMinuteOpen(open[4]);
	
					schedulerModel.setDateClosed(closed[0]);
					schedulerModel.setMonthClosed(closed[1]);
					schedulerModel.setYearClosed(closed[2]);
					schedulerModel.setHourClosed(closed[3]);
					schedulerModel.setMinuteClosed(closed[4]);
					System.out.println("FOR TRAINEE");
					Action(schedulerModel);
				}
			}
		}
		}catch(Exception e){
			e.printStackTrace();
		}
//		List result
		try{
		result=schedulerManager.selectExamCandidate();
		if(result.size()!=0){
			for(int x=0;x<result.size();x++){
				QuartzModel schedulerModel=(QuartzModel) result.get(x);
				
				String[] open = schedulerModel.getStartHour().split(":");
				schedulerModel.setExamFor("Candidate");
		
				schedulerModel.setDateOpen(open[0]);
				schedulerModel.setMonthOpen(open[1]);
				schedulerModel.setYearOpen(open[2]);
				schedulerModel.setHourOpen("23");
				schedulerModel.setMinuteOpen("59");
				
				System.out.println("FOR CANDIDATE");
				Action(schedulerModel);
			}
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		//reload exam for candidate
		try{
		result=schedulerManager.reloadExamCandidate();
		if(result.size()!=0){
			for(int x=0;x<result.size();x++){
				QuartzModel schedulerModel=(QuartzModel) result.get(x);
				
				String[] open = schedulerModel.getStartHour().split(":");
				schedulerModel.setExamFor("ReloadCandidate");
				schedulerModel.setDateClosed(open[0]);
				schedulerModel.setMonthClosed(open[1]);
				schedulerModel.setYearClosed(open[2]);
				schedulerModel.setHourClosed(open[3]);
				schedulerModel.setMinuteClosed(open[4]);
				
				System.out.println("FOR Reload exam CANDIDATE");
				ActionCandidate(schedulerModel);
			}
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		try{
			 ActionResetRegNo();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		System.out.println("YUUUUUUUUUUUUUUUUUUUUUUKKKKK");
		

	}

}